import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
import 'package:image_picker/image_picker.dart';

import '../profile/repository/bt_profile_repository.dart';
import 'bt_file_upload.dart';

showImagePickerModal(BuildContext context, Function(String?) onResult) {
  ImagePicker picker = ImagePicker();
  BtProfileRepository repository = BtProfileRepository();
  showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      builder: (_) => PointerInterceptor(
            child: Container(
              height: 158,
              decoration: BoxDecoration(
                color: const Color(0xFFFFFFFF),
                borderRadius: BorderRadius.circular(6),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  InkWell(
                    child: Container(
                      height: 46,
                      decoration: const BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            color: Color(0xFFEEEEEE),
                          ),
                        ),
                      ),
                      child: const Center(
                        child: Text(
                          "Take a photo",
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Color(0xFF222222), fontSize: 15),
                        ),
                      ),
                    ),
                    onTap: () async {
                      if (!await Permission.photos.isGranted) {
                        var permissionStatus = await Permission.photos.request();
                        if (permissionStatus != PermissionStatus.granted) {
                          EasyLoading.showToast("You have not authorised this permission.");
                          return;
                        }
                      }

                      final XFile? image = await picker.pickImage(source: ImageSource.camera);
                      if (null != image) {
                        EasyLoading.show(status: 'Loading...');
                        repository.fetchOssPolicy((ossPolicy) async {
                          String filename = await fileUpload(image, ossPolicy!);
                          repository.updateAvatar(filename, (success, msg, data) {
                            if (success) {
                              onResult(data?.thumbUrl);
                              Navigator.of(context).pop();
                            }
                            EasyLoading.showToast(msg);
                          });
                        });
                      }
                    },
                  ),
                  InkWell(
                    child: const SizedBox(
                      height: 46,
                      child: Center(
                        child: Text(
                          "Photo album",
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Color(0xFF222222), fontSize: 15),
                        ),
                      ),
                    ),
                    onTap: () async {
                      if (!await Permission.photos.isGranted) {
                        var permissionStatus = await Permission.camera.request();
                        if (permissionStatus != PermissionStatus.granted) {
                          EasyLoading.showToast("You have not authorised this permission.");
                          return;
                        }
                      }

                      final XFile? image = await picker.pickImage(source: ImageSource.gallery);
                      if (null != image) {
                        EasyLoading.show(status: 'Loading...');
                        repository.fetchOssPolicy((ossPolicy) async {
                          String filename = await fileUpload(image, ossPolicy!);
                          repository.updateAvatar(filename, (success, msg, data) {
                            if (success) {
                              onResult(data?.thumbUrl);
                              Navigator.of(context).pop();
                            }
                            EasyLoading.showToast(msg);
                          });
                        });
                      }
                    },
                  ),
                  const Divider(
                    thickness: 8,
                    color: Color(0xFFF6F7F8),
                  ),
                  InkWell(
                    child: const SizedBox(
                      height: 46,
                      child: Center(
                        child: Text(
                          "Cancel",
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Color(0xFF222222), fontSize: 15),
                        ),
                      ),
                    ),
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              ),
            ),
          ));
}
